package com.questionnaire.controller;

import com.github.pagehelper.PageInfo;
import com.questionnaire.entity.*;
import com.questionnaire.service.AnswerService;
import com.questionnaire.service.QuestionService;
import com.questionnaire.service.QuestionnaireService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.Arrays;
import java.util.List;

@Controller
public class StatisticsController {
    @Autowired
    private QuestionService questionService;
    @Autowired
    private QuestionnaireService questionnaireService;
    @Autowired
    private AnswerService answerService;

    @RequestMapping("/statistics.do")
    public String statistics(ModelMap map, Integer nid){
        Questionnaire questionnaire=null;

        if(nid==null){
            questionnaire = questionnaireService.selectLast();
            nid=questionnaire.getNid();
        }else {
             questionnaire = questionnaireService.selectByPrimaryKey(nid);
        }

        QuestionExample example=new QuestionExample();
        QuestionExample.Criteria criteria = example.createCriteria();
        criteria.andOwnerquestionnaireidEqualTo(nid);
        List<Question> questions= questionService.selectByExample(example);
        PageInfo<Question> questionsInfo=new PageInfo<Question>(questions);

        AnswersExample example01=new AnswersExample();
        AnswersExample.Criteria criteria01 = example01.createCriteria();
        criteria01.andOwnerquestionnaireidEqualTo(nid);
        List<Answers> answers = answerService.selectByExample(example01);

        int sum=answers.size()/questions.size();

        map.put("sum",sum);
        map.put("questionnaire",questionnaire);
        map.put("questionsInfo",questionsInfo);
        return "statistics";
    }


    @RequestMapping(value = "/statisticsQuestion.do")
    public String   statisticsQuestion(ModelMap map,Integer nid, Integer qid ,Integer sum) {

        QuestionExample example1=new QuestionExample();
        QuestionExample.Criteria criteria1 = example1.createCriteria();
        criteria1.andOwnerquestionnaireidEqualTo(nid);
        List<Question> questions= questionService.selectByExample(example1);
        PageInfo<Question> questionsInfo=new PageInfo<Question>(questions);

        Questionnaire questionnaire = questionnaireService.selectByPrimaryKey(nid);
        AnswersExample example=new AnswersExample();
        AnswersExample.Criteria criteria = example.createCriteria();
        criteria.andOwnerquestionidEqualTo(qid);
        criteria.andOwnerquestionnaireidEqualTo(nid);
        List<Answers> answers = answerService.selectByExample(example);
        PageInfo<Answers> answersInfo=new PageInfo<Answers>(answers);

        Integer [] statistic= {0,0,0,0};
        answers.forEach(answer -> {
                String options = answer.getOptions();
                if(options==null){
                    options="";
                }
                if(options.indexOf("A")!=-1){
                    statistic[0]=statistic[0]+1;
                }
                if(options.indexOf("B")!=-1){
                    statistic[1]=statistic[1]+1;
                }
                if(options.indexOf("C")!=-1){
                    statistic[2]=statistic[2]+1;
                }
                if(options.indexOf("D")!=-1){
                    statistic[3]=statistic[3]+1;
                }
        });
        int flug=0;
        for (int i = 0; i <statistic.length ; i++) {
            if (statistic[i]!=0){
                flug=1;

                break;
            }
        }

        List<Integer>  answerstatistic= Arrays.asList(statistic);

        map.put("qid",qid);
        map.put("answersInfo",answersInfo);
        map.put("answerstatistic",answerstatistic);
        map.put("flug",flug);
        map.put("questionnaire",questionnaire);
        map.put("sum",sum);
        map.put("questionsInfo",questionsInfo);
        return "statisticsQuestion";
    }


}
